package com.ruoyi.web.controller.crm;

import com.bonuo.crm.domain.Activity;
import com.bonuo.crm.domain.Agent;
import com.bonuo.crm.service.ActivityService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

//活动记录Controller
@RestController
@RequestMapping("/crm/activity")
@Api(tags = "活动记录管理")
public class ActivityController extends BaseController{
    @Autowired
    private ActivityService activityService;

    //查询活动记录
    @ApiOperation(value = "查询活动记录",httpMethod = "GET")
    @GetMapping("/list")
    public TableDataInfo selectActivityList(Activity activity,Integer pageNum,Integer pageSize){
        if(pageNum!=null && pageSize!=null){
            startPage();
        }
        List<Activity> activityList = activityService.selectActivityList(activity);
        return getDataTable(activityList);
    }

    //添加活动记录
    @ApiOperation(value = "添加活动记录",httpMethod = "POST")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Activity activity){
        //查询类型和内容是否相同，相同的话那就不能提添加
        Integer count = activityService.selectRepeatActivity(activity);
        if(count >= 1){
            return AjaxResult.warn("该类型和内容已经存在，不能重复添加");
        }
        Integer add = activityService.insertActivity(activity);
        return toAjax(add);
    }

    //删除活动记录
    @ApiOperation(value = "删除活动记录",httpMethod = "DELETE")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id){
        Integer delete = activityService.deleteActivityById(id);
        return toAjax(delete);
    }

    //修改活动记录
    @ApiOperation(value = "修改活动记录",httpMethod = "PUT")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Activity activity){
        Integer update = activityService.updateActivity(activity);
        return toAjax(update);
    }

    //导出活动记录
    @ApiOperation(value = "导出活动记录",httpMethod = "POST")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Activity activity) {
        List<Activity> list = activityService.selectActivityList(activity);
        ExcelUtil<Activity> util = new ExcelUtil<Activity>(Activity.class);
        util.exportExcel(response, list, "日常活动记录数据");
    }

    //crm首页
    @GetMapping("/crmIndex")
    @ApiOperation(value = "crm首页",httpMethod = "GET")
    public AjaxResult crmIndex(Integer userId){
        Map<String,Object> map = activityService.crmIndex(userId);
        return AjaxResult.success(map);
    }

}
